﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using Koonsoft.HYCRM.DAL;
using Koonsoft.HYCRM.Model;

namespace Koonsoft.HYCRM.BLL
{
    public class HKCompanyBLL
    {

        public Dictionary<string, List<HKCompanyInfo>> ChackCompanyName(string EnglishName, string ChineseName)
        {
            HttpHelper help = new HttpHelper();

            #region 伪造Cookie，语言为简体中文
            Dictionary<string, string> dic2 = new Dictionary<string, string>();
            dic2.Add("web.country", "CN");
            dic2.Add("web.language", "zh");

            help.Channel("http://www.icris.cr.gov.hk", dic2);
            #endregion

            string str = help.Get("https://www.icris.cr.gov.hk/csci/login_i.do?loginType=iguest&username=iguest");
            str = help.Get("https://www.icris.cr.gov.hk/csci/cps_criteria.jsp");


            Dictionary<string, List<HKCompanyInfo>> list = new Dictionary<string, List<HKCompanyInfo>>();


            list.Add("en", GetCompany(EnglishName, "en"));
            list.Add("ch", GetCompany(ChineseName, "ch"));


            return list;
        }

        private List<HKCompanyInfo> GetCompany(string companyName, string language)
        {
            if(string.IsNullOrEmpty(companyName))
            {
                return new List<HKCompanyInfo>();
            }

            HttpHelper help = new HttpHelper();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("nextAction", "cps_criteria");
            dic.Add("searchPage", "True");
            dic.Add("DPDSInd", "true");
            dic.Add("searchMode", "BYNAME");
            dic.Add("radioButton", "BYNAME");
            dic.Add("CRNo", "");
            dic.Add("mode", "EXACT+NAME");
            dic.Add("showMedium", "true");
            dic.Add("companyName", companyName);
            dic.Add("language", language);
            dic.Add("page", "1");

            string str = help.Post("https://www.icris.cr.gov.hk/csci/cps_criteria.do", dic);
            if (str.Contains("没有纪录与输入的查询资料相符"))
            {
                return new List<HKCompanyInfo>();
            }
            else
            {
                var doc = new HtmlDocument();
                doc.LoadHtml(str);
                var rootNode = doc.DocumentNode;
                List<HKCompanyInfo> list = new List<HKCompanyInfo>();

                for (int i = 2; i <= rootNode.SelectNodes("./html[1]/body[1]/div[2]/table[3]/tr[1]/td[1]/table[1]/tr").Count; i++)
                {
                    if (rootNode.SelectNodes("./html[1]/body[1]/div[2]/table[3]/tr[1]/td[1]/table[1]/tr[" + i + "]/td").Count > 1)
                    {

                        var company = new HKCompanyInfo();
                        company.CompanyID =
                            RepaceTag(
                                rootNode.SelectNodes(
                                    string.Format(
                                        "./html[1]/body[1]/div[2]/table[3]/tr[1]/td[1]/table[1]/tr[{0}]/td[2]", i))[0].
                                    InnerText);

                        company.CompanyName = RepaceTag(
                                rootNode.SelectNodes(
                                    string.Format(
                                        "./html[1]/body[1]/div[2]/table[3]/tr[1]/td[1]/table[1]/tr[{0}]/td[3]", i))[0].
                                    InnerText);

                        company.ActiveStatus = RepaceTag(
                              rootNode.SelectNodes(
                                  string.Format(
                                      "./html[1]/body[1]/div[2]/table[3]/tr[1]/td[1]/table[1]/tr[{0}]/td[6]", i))[0].
                                  InnerText);

                        list.Add(company);
                    }

                }



                return list;
            }
        }


        private string RepaceTag(string text)
        {
            var content1 = Regex.Replace(text, @"[\r\n\t]", string.Empty).Trim();
            var content = Regex.Replace(content1, @"<[\S\s]*?>", string.Empty).Trim();

            if (content == "-")
            {
                return string.Empty;
            }

            return content;
        }
    }
}
